<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<html>
<head>
    <meta charset="utf-8" />
    <title th:replace="common/base::pagetitle"></title>
    <link th:replace="common/base::static"/>
    <link rel='stylesheet' th:href="@{/static/css/bootstrap-combined.min.css}">
    <link rel='stylesheet' id='icon_stylesheet' href="https://cdn.bootcdn.net/ajax/libs/font-awesome/4.0.3/css/font-awesome.min.css">
    <script th:src="@{/static/js/jsoneditor.min.js}"></script>
    <script th:src="@{/static/js/jquery.min.js}"></script>
    <style>[class*="foundicon-"] {font-family: GeneralFoundicons;font-style: normal;}</style>
    <!-- loa上下文路径  -->
    <script th:fragment="context" th:inline="javascript">
        var BASE_WEB = [[${#httpServletRequest.getContextPath()}]] + '/';
    </script>
</head>
<body>

<div class='container'>
    <input id="businessId" name="businessId" th:value="${businessId}" hidden="hidden">
    <input id="btype" name="btype" th:value="${btype}" hidden="hidden">
    <div class='row'>
        <div class='span8 col-md-8 columns eight large-8' style="">
            <!--<h2>模板</h2>-->
            <div id='editor' ></div>
        </div>
    </div>
</div>
<script>
    (function () {
        //json schema模板
        var btype = $('#btype').val();
        var businessId = $('#businessId').val();
        console.log("btype",btype);
        var schema = '';
        $.ajax({
            type: 'get',
            url: BASE_WEB + 'block/info/schema?templateBtype='+btype,
            async: false,
            success: function (r) {
                if (r.code === 200) {
                    schema = r.data;
                } else {
                    schema = {};
                }
            }
        });

        // Divs/textareas on the page
        var $editor = document.getElementById('editor');
        var jsoneditor;
        //加载json schema，渲染表单
        var reload = function (keep_value) {
            console.log("reload.keep_value" + keep_value);
            var startval = (jsoneditor && keep_value) ? jsoneditor.getValue() : window.startval;
            window.startval = undefined;
            //如果存在，先销毁
            if (jsoneditor) jsoneditor.destroy();
            //初始化jsoneditor
            jsoneditor = new JSONEditor($editor, {
                schema: schema,
                startval: startval
            });
            window.jsoneditor = jsoneditor;
            // 当编辑器的值更改时，更新JSON输出和验证消息
            var data = '';
            $.ajax({
                type: 'get',
                url: BASE_WEB + 'block/info/data?id='+businessId,
                async: false,
                success: function (r) {
                    if (r.code === 200) {
                        console.log("data..........", r.data);
                        data = r.data;
                    } else {
                        data ='';
                    }
                }
            });
            jsoneditor.setValue(data);
        };


        // 设置样式
        var setTheme = function (theme, no_reload) {
            JSONEditor.defaults.options.theme = 'bootstrap2';
            if (!no_reload) reload(true);
        };

        // 设置图标样式
        var setIconlib = function (iconlib, no_reload) {
            JSONEditor.defaults.options.iconlib = 'fontawesome4';
            if (!no_reload) reload(true);
        };

        // Set options from direct link
        setTheme('bootstrap2', true);

        setIconlib('fontawesome4', true);

        JSONEditor.defaults.options.object_layout = 'normal';

        JSONEditor.defaults.options.show_errors = 'interaction';

        reload();
    })();

    (function (i, s, o, g, r, a, m) {
        i['GoogleAnalyticsObject'] = r;
        i[r] = i[r] || function () {
            (i[r].q = i[r].q || []).push(arguments)
        }, i[r].l = 1 * new Date();
        a = s.createElement(o),
            m = s.getElementsByTagName(o)[0];
        a.async = 1;
        a.src = g;
        m.parentNode.insertBefore(a, m)
    })(window, document, 'script', 'https://cdn.bootcdn.net/ajax/libs/analytics.js/2.9.1/analytics.js', 'ga');

    ga('create', 'UA-17940230-1', 'jeremydorn.com');
    ga('send', 'pageview');
</script>


</body>
</html>